package com.kingbook.core.generator;

import com.kingbook.common.utils.StringUtils;
import com.kingbook.core.exception.ParamsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * @Author: Bucky
 * @Date: 2019/1/2
 * @Version 1.0
 */
public class ServiceClassGenerator extends Generator {

    private static final Logger logger = LoggerFactory.getLogger(ServiceClassGenerator.class);

    private String target;

    private String packages;

    private String javaBeanPackage;

    private List<TableBean> tables;

    private AnnotationInfo info;

    ServiceClassGenerator(String target, String packages, String javaBeanPackage, List<TableBean> tables,AnnotationInfo info,boolean overWrite) {
        this.target = target + "/" + packages.replace(".", "/");
        this.packages = packages;
        this.javaBeanPackage = javaBeanPackage;
        this.tables = tables;
        this.info = info;
        setOverWrite(overWrite);
    }

    void gen() {
        String template = readTemplate("classService.tpl");
        String javaBeanClass;
        Map<String, PrimaryKey> primaryKeyMap;
        String tTemplate;
        for (TableBean bean : tables) {
            if (bean.isCreateService()) {
                javaBeanClass = bean.getObjectName();
                primaryKeyMap = bean.getPrimaryKeys();
                tTemplate = template;

                writeTemplate(target + "/" + javaBeanClass + "Service.java",
                        tTemplate.replace("${author}", info.getAuthor())
                                .replace("${time}", info.getTime())
                                .replace("${version}", info.getVersion())
                                .replace("${packages}", packages)
                                .replace("${javaBeanClass}", javaBeanClass));
            }

        }

    }

    private boolean hasUUID(Map<String, PrimaryKey> primaryKey) {
        for (PrimaryKey pk : primaryKey.values()) {
            switch (pk.getPrimaryKeyType()) {
                case UUID10:
                case UUID16:
                case UUID22:
                case UUID32:
                    return true;
                default:
            }
        }
        return false;
    }

    private String genPkMethod(PrimaryKey primaryKey) {
        String col = "\n        param.put(\"" + StringUtils.toCamelCase(primaryKey.getColumn()) + "\", ";
        switch (primaryKey.getPrimaryKeyType()) {
            case UUID10:
                return col + "NumberUtils.uuid10()); ";
            case UUID16:
                return col + "NumberUtils.uuid16()); ";
            case UUID22:
                return col + "NumberUtils.uuid22()); ";
            case UUID32:
                return col + "NumberUtils.uuid()); ";
            case NANO_SECONDS:
                return col + "System.nanoTime()); ";
            case SEQUENCE:
                if (StringUtils.isEmpty(primaryKey.getSequenceName()))
                    throw new ParamsException("----> 没有设置SequenceName, column=" + primaryKey.getColumn());
                return col + "nextSequence(\"" + primaryKey.getSequenceName() + "\")); ";
            default:
                return "";
        }
    }


}
